﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using Sunboxsoft.ASMS.Client.UI.Properties;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
namespace Sunboxsoft.ASMS.Client.UI.Management
{
    public partial class PayItemListView : UserControl, IView
    {
        //当前页  
        private int _pageIndex = 0;
        //总共几页
        private int _pageCount = 0;

        public PayItemListView()
        {
            InitializeComponent();
            this.GCData.EmbeddedNavigator.ButtonClick += new DevExpress.XtraEditors.NavigatorButtonClickEventHandler(EmbeddedNavigator_ButtonClick);
        }

        #region IView 成员

        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("create", Resources.isAdd, "新增", btnAdd_Click))
                .AddAction(new ActionNode("delete", Resources.isDelete, "删除", btnDelete_Click))
                .AddAction(new ActionNode("refresh", Resources.isRefresh, "刷新", btnRefresh_Click));
        }

        public void Resume(ViewResumeArgs args)
        {
            if (args.ClosedArgs.Result)
            {
                BindData();
            }
        }
        public void Active(ViewActiveArgs args)
        {
            BindType();
            BindState();
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("");
        }
        #endregion

        #region Method
        /// <summary>
        /// 绑定类型
        /// </summary>
        private void BindType()
        {
            this.lupType.Properties.DataSource = Domain.Constant.PayItemTypes.Enumerate();
            this.lupType.Properties.DisplayMember = "Text";
            this.lupType.ItemIndex = 0;
        }

        /// <summary>
        /// 绑定状态
        /// </summary>
        private void BindState()
        {
            this.lupState.Properties.DataSource = Domain.Constant.CommonStatus.Enumerate();
            this.lupState.Properties.DisplayMember = "Text";
            this.lupState.ItemIndex = 0;
        }

        /// <summary>
        /// 获取选中的类别
        /// </summary>
        /// <returns></returns>
        public int GetStatus()
        {
            Domain.IntCodeTextItem item = this.lupState.GetSelectedDataRow() as Domain.IntCodeTextItem;
            if (item == null)
            {
                return Domain.Constant.BookingBillStatus.All;
            }
            return item.Code;
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        private void BindData()
        {
            this.ShowMask("loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                PagedResult<WebRef.EPayItem> result = new Service.QueryService().QueryPayItemPagedList(
                    ModuleContext.Current.BusinessId,
                    ModuleContext.Current.StationId,
                    GetStatus(),
                    _pageIndex * GetPageLimit(),
                    GetPageLimit());
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        GCData.Paginate<WebRef.EPayItem>(result, out _pageIndex, out _pageCount);
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError("查询失败！");
                    }
                    this.HideMask();
                }));
            });
        }

        /// <summary>
        /// 获取每页条数限制
        /// </summary>
        /// <returns></returns>
        private int GetPageLimit()
        {
            int pagesize = string.IsNullOrEmpty(txtPageSize.Text.Trim()) == true ? 10 : Convert.ToInt32(txtPageSize.Text.Trim());
            return pagesize;
        }
        #endregion

        #region Action
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, object e)
        {
            UIContext.Current.ViewManager.PopupView(new CreatePayItemView(), new PopupViewArgs("新增付费项目"));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, object e)
        {

        }

        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, object e)
        {
            _pageIndex = 0;
            BindData();
        }
        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            e.UpdatePageIndex(_pageCount, ref _pageIndex);
            BindData();
        }
        #endregion
    }
}
